﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CuocChienQueNha.Class.Character.army
{
    class BasicArmy
    {
        #region
        public string Loai { get; set; } //Tên của các loại lính (archer,knight,shraman,sword)
        public int level { get; set; } /* mức cơ bản, trưởng thành, nâng cấp */
        public int sosao { get; set; } /* mức nâng cấp sao của tướng */
        public string huong { get; set; } /* hướng đứng của tướng */
        public Vector2 position { get; set; } /* vị trí của tướng */
        protected Vector2 Lechposition; /* độ lệch so với vị trí để vẽ tướng chuẩn hơn */
        protected Vector2 sizeImages;  /* kích thước bảng ảnh */
        public Texture2D image, imgLock, imgStar; /* ảnh hiển thị */
        protected Vector2 index; /* vị trí tướng trong map */
        public bool isVisible { get; set; }/* lính tồn tại */
        Random rd;
        int count;
        #endregion
        /* Khởi tạo lính */

        public BasicArmy()
        {
            rd = new Random();
            sosao = 1;
            count = 0;
            sosao = 1;
            huong = "Bottom";
            Lechposition = new Vector2(0, 0);
            position = new Vector2(0, 0);
            index = new Vector2(0, 0);
        }

        public BasicArmy(string loai, int level)
        {
            setLoai(loai, level);
        }

        /* đặt các thuộc tính cho lính */
        private void setLoai(string loai, int level)
        {
            sosao = 1;
            this.level = level;
            count = 0;
            sosao = 3;
            this.Loai = loai;
            huong = "Left";
            Lechposition = new Vector2(0, 0);
            position = new Vector2(0, 0);
            index = new Vector2(0, 0);
        }
        /* get, set các chỉ số */
        public void setIndex(Vector2 chiso)
        {
            index = chiso;
            position = new Vector2(chiso.Y * 40, chiso.X * 40);
            isVisible = true;
        }

        /* load ảnh cho lính */
        public virtual void Load(ContentManager cont)
        {
            if (Loai.Equals("sword"))
            {
                LoadSword(cont);
            }
            else if (Loai.Equals("knight"))
            {
                LoadKnight(cont);
            }
            else if (Loai.Equals("archer"))
            {
                LoadArcher(cont);
            }
            else
            {
                LoadSharman(cont);
            }
            imgLock = cont.Load<Texture2D>("Images/MainMap/Lock");
            imgStar = cont.Load<Texture2D>("Images/MainMap/star");
        }
        public void LoadSword(ContentManager cont)
        {
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/Sword_" + level + "1");
            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/Sword_" + level + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/Sword_" + level + "2");
            }

            if (level == 0)
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(380, 64);
                    Lechposition = new Vector2(-15, -30);
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(224, 62);
                    Lechposition = new Vector2(-10, -30);
                }
                else
                {
                    sizeImages = new Vector2(370, 68);
                    Lechposition = new Vector2(-15, -30);
                }
                #endregion
            }
            else
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(370, 68);
                    Lechposition = new Vector2(-20, -30);
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(292, 62);
                    Lechposition = new Vector2(-15, -30);
                }
                else
                {
                    sizeImages = new Vector2(370, 68);
                    Lechposition = new Vector2(-15, -30);
                }
                #endregion
            }
        }//complete
        public void LoadKnight(ContentManager cont)
        {
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/knight_" + level + "1");
            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/knight_" + level + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/knight_" + level + "2");
            }
            #region
            if (huong.Equals("Bottom"))
            {
                sizeImages = new Vector2(224, 58);
                Lechposition = new Vector2(-15, -30);
            }
            else
            {
                sizeImages = new Vector2(280, 58);
                Lechposition = new Vector2(-15, -30);
            }
            #endregion
        }
        public void LoadArcher(ContentManager cont)
        {
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/archer_" + level + "1");

            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/archer_" + level + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/archer_" + level + "2");
            }

            if (level == 0)
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(225, 58);
                    Lechposition = new Vector2(-10, -30);
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(224, 58);
                    Lechposition = new Vector2(-10, -30);
                }
                else
                {
                    sizeImages = new Vector2(280, 58);
                    Lechposition = new Vector2(-10, -30);
                }
                #endregion
            }
            else
            {
                #region
                if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(224, 62);
                    Lechposition = new Vector2(-15, -30);
                }
                else
                {
                    sizeImages = new Vector2(280, 64);
                    Lechposition = new Vector2(-15, -30);

                }
                #endregion
            }
        }
        public void LoadSharman(ContentManager cont)
        {
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/shraman_" + level + "1");
            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/shraman_" + level + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("Images/Character/Armys/shraman_" + level + "2");
            }
            #region
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                sizeImages = new Vector2(level == 2 ? 300 : 280, 68);
                Lechposition = new Vector2(-15, -30);
            }
            else if (huong.Equals("Bottom"))
            {
                sizeImages = new Vector2(240, 62);
                Lechposition = new Vector2(-15, -30);
            }
            else
            {
                sizeImages = new Vector2(300, 68);
                Lechposition = new Vector2(-15, -30);
            }
            #endregion
        }
        /* vẻ ảnh lính */

        public virtual void Draw(SpriteBatch sprite, SpriteFont font, bool visibleStar = true)
        {
            #region
            if (isVisible)
            {
                if (huong.Equals("Bottom"))
                {
                    if (visibleStar) //isNang cap la o trong cua hang
                    {
                        sprite.Draw(image, position + Lechposition, new Rectangle((int)sizeImages.X / 4 * (count / 5), 0, (int)sizeImages.X / 4, (int)sizeImages.Y),
                            Color.White * (visibleStar == true ? 1.0f : 1), 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                    }
                    else
                    {
                        if (imgLock != null)
                            sprite.Draw(imgLock, position * 1 + new Vector2(50, 50), new Rectangle(0, 0, 82, 99),
                                Color.White, 0, Vector2.Zero, 0.5f, SpriteEffects.None, 0);
                    }
                    if (!GamePage.isPause) count++;
                    if (count >= 20) count = 0;
                }
                else
                {
                    if (huong.Equals("Left") || huong.Equals("TopLeft"))
                    {
                        sprite.Draw(image, position + Lechposition, new Rectangle((int)sizeImages.X / 5 * (count / 5), 0, (int)sizeImages.X / 5, (int)sizeImages.Y),
                            Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                    }
                    else
                    {
                        //sprite.Draw(image, position * 1 + Lechposition, new Rectangle((int)sizeImages.X / 5 * (count / 2 > 4 ? 0 : count / 2), 0, (int)sizeImages.X / 5, (int)sizeImages.Y),
                        //                        Color.White, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);
                        sprite.Draw(image, position + Lechposition, new Rectangle((int)sizeImages.X / 5 * (count / 6), 0, (int)sizeImages.X / 5, (int)sizeImages.Y),
                            Color.White, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);
                    }
                    if (!GamePage.isPause) count++;
                    if (count >= 25) count = 0;
                }
                if (visibleStar) //  vẽ sao
                {
                    for (int k = 1; k <= sosao; k++)
                    {
                        sprite.Draw(imgStar, position * 1 + new Vector2(8 + 11 * k, -25), new Rectangle(0, 0, 32, 32),
                                                     Color.White, 0, Vector2.Zero, 0.4f, SpriteEffects.None, 0);
                    }
                }
            }
            #endregion
        }

        public virtual void DrawInformation(SpriteBatch sprite, SpriteFont font, int loai, int lever)
        {
            Info info = getInfo(loai, lever);
            sprite.DrawString(font, info.name, new Vector2(550, 100), Color.Yellow, 0, Vector2.Zero, 1.5f, SpriteEffects.None, 0);
            sprite.DrawString(font, "Sát thương : " + info.satthuong, new Vector2(500, 150), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            sprite.DrawString(font, "Tốc độ : " + info.tocdo, new Vector2(500, 200), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            sprite.DrawString(font, "Tầm xa : " + info.tamxa, new Vector2(500, 250), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
            sprite.DrawString(font, "Giá : " + info.gia, new Vector2(500, 300), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
        }
        /* hành động của lính */
        public void Ban(List<Enemy> enemy)
        {
            bool dangban = false;
            if (isVisible)
            {
                for (int j = 0; j < enemy.Count; j++)
                {
                    //if (GamePage.save.GetValue(Loai, level, sosao).getTamXa() * 1 >= (float)KhoangCach(enemy[j]))
                    //{
                    //    Quay(enemy[j]);
                    //    dangban = true;
                    //    if (count % ((int)(40 / GamePage.save.GetValue(Loai, level, sosao).getTocdo())) == 4)
                    //    {
                    //        enemy[j].setTruMau(GamePage.save.GetValue(Loai, level, sosao).getSatthuong(),
                    //            (rd.Next(1000) % 100 <= GamePage.save.GetValue(Loai, level, sosao).getTiLe()) ? GamePage.save.GetValue(Loai, level, sosao).getHieuUng() : "");
                    //        MainPage.PlayHieuUng("one_handed", false);
                    //    }
                    //    break;
                    //}
                }
            }
            if (dangban == false)
            {
                huong = "Bottom";
            }
        }
        public float KhoangCach(Enemy enemy)
        {
            return Vector2.Distance(position, enemy.position);
        }
        public void Quay(Enemy enemy)
        {
            if (enemy.index.X < index.X)
            {
                if (enemy.index.Y >= index.Y)
                {
                    huong = "TopRight";
                }
                else
                {
                    huong = "TopLeft";
                }
            }
            else
            {
                if (enemy.index.Y >= index.Y)
                {
                    huong = "Right";
                }
                else
                {
                    huong = "Left";
                }
            }
        }

        public bool isTouched(Vector2 posi)
        {
            if (posi.X >= position.X && posi.X <= (position.X + 40) && posi.Y >= position.Y && posi.Y <= (position.Y + 40)) return true;
            else return false;
        }

        public Info getInfo(string loai, int level, int sao)
        {
            if (loai.Equals("sword"))
            {
                return getInfo(level, sao);
            }
            else if (loai.Equals("archer"))
            {
                return getInfo(3 + level, sao);
            }
            else if (loai.Equals("knight"))
            {
                return getInfo(6 + level, sao);
            }
            else if (loai.Equals("shaman"))
            {
                return getInfo(9 + level, sao);
            }
            return getInfo(12, sao);
        }
        public Info getInfo(int loai, int lever)
        {
            Info info = new Info();
            if (loai == 0)
            {
                info.setHieuUng("", 0);
                ////info.setName("Kiếm sĩ");
                if (lever == 1)
                {
                    info.set(10, 1.0f, 192, 0);
                }
                else if (lever == 2)
                {
                    info.set(12, 1.0f, 208, 0);
                }
                else
                {
                    info.set(14, 1.3f, 224, 0);
                }
            }
            else if (loai == 1)
            {
                info.setHieuUng("lam do", 20);
                //info.setName("Vô ảnh kiếm");
                if (lever == 1)
                {
                    info.set(14, 1.2f, 240, 0);
                }
                else if (lever == 2)
                {
                    info.set(17, 1.4f, 256, 0);
                }
                else
                {
                    info.set(20, 1.6f, 272, 0);
                }
            }
            else if (loai == 2)
            {
                info.setHieuUng("dot tu", 10);
                //info.setName("Đao vương");
                if (lever == 1)
                {
                    info.set(16, 1.3f, 288, 5000);
                }
                else if (lever == 2)
                {
                    info.set(20, 1.4f, 304, 5000);
                }
                else
                {
                    info.set(22, 1.5f, 320, 5000);
                }
            }
            else if (loai == 3)
            {
                info.setHieuUng("", 0);
                //info.setName("Xạ thủ");
                if (lever == 1)
                {
                    info.set(5, 1.9f, 448, 0);
                }
                else if (lever == 2)
                {
                    info.set(8, 2.1f, 464, 0);
                }
                else
                {
                    info.set(9, 2.1f, 480, 0);
                }
            }
            else if (loai == 4)
            {
                info.setHieuUng("xuyen giap", 20);
                //info.setName("Xạ thần");
                if (lever == 1)
                {
                    info.set(8, 2.3f, 496, 0);
                }
                else if (lever == 2)
                {
                    info.set(10, 2.5f, 512, 0);
                }
                else
                {
                    info.set(12, 2.4f, 528, 0);
                }
            }
            else if (loai == 5)
            {
                info.setHieuUng("chi mang", 25);
                //info.setName("Bá vương tiễn");
                if (lever == 1)
                {
                    info.set(13, 2.2f, 544, 20000);
                }
                else if (lever == 2)
                {
                    info.set(15, 2.6f, 560, 20000);
                }
                else
                {
                    info.set(17, 2.6f, 576, 20000);
                }
            }
            else if (loai == 6)
            {
                info.setHieuUng("", 0);
                //info.setName("Thủ sĩ");
                if (lever == 1)
                {
                    info.set(20, 0.7f, 256, 0);
                }
                else if (lever == 2)
                {
                    info.set(24, 0.8f, 272, 0);
                }
                else
                {
                    info.set(28, 0.8f, 288, 0);
                }
            }
            else if (loai == 7)
            {
                info.setHieuUng("xuyen giap", 20);
                //info.setName("Thần thương");
                if (lever == 1)
                {
                    info.set(24, 0.7f, 304, 0);
                }
                else if (lever == 2)
                {
                    info.set(30, 0.9f, 320, 0);
                }
                else
                {
                    info.set(33, 1.0f, 336, 0);
                }
            }
            else if (loai == 8)
            {
                info.setHieuUng("chi mang", 25);
                //info.setName("Kim đại phủ");
                if (lever == 1)
                {
                    info.set(28, 1.1f, 352, 15000);
                }
                else if (lever == 2)
                {
                    info.set(35, 1.3f, 368, 15000);
                }
                else
                {
                    info.set(37, 1.5f, 384, 15000);
                }
            }
            else if (loai == 9)
            {
                info.setHieuUng("", 0);
                //info.setName("Phiến vũ");
                if (lever == 1)
                {
                    info.set(5, 1.7f, 576, 0);
                }
                else if (lever == 2)
                {
                    info.set(7, 1.9f, 592, 0);
                }
                else
                {
                    info.set(8, 1.9f, 608, 0);
                }
            }
            else if (loai == 10)
            {
                info.setHieuUng("lam cham", 20);
                //info.setName("Thủy trượng sư");
                if (lever == 1)
                {
                    info.set(7, 1.8f, 624, 20000);
                }
                else if (lever == 2)
                {
                    info.set(9, 2.0f, 640, 20000);
                }
                else
                {
                    info.set(9, 2.3f, 665, 20000);
                }
            }
            else if (loai == 11)
            {
                info.setHieuUng("chay mau", 25);
                //info.setName("Hỏa trượng sư");
                if (lever == 1)
                {
                    info.set(8, 1.9f, 672, 20000);
                }
                else if (lever == 2)
                {
                    info.set(9, 2.5f, 688, 20000);
                }
                else
                {
                    info.set(11, 2.2f, 704, 20000);
                }
            }
            else
            {
                info.setHieuUng("dot tu", 20);
                info.set(50, 1.5f, 384, 0);
            }
            return info;
        }
    }
    public class Info
    {
        public string name { get; set; }
        public float satthuong { get; set; }
        public float tocdo { get; set; }
        public float tamxa { get; set; }
        public float gia { get; set; }
        public string hieuung { get; set; }
        public int tile { get; set; }
        public Info()
        {
            name = "";
            satthuong = 0;
            tocdo = 0;
            tamxa = 0;
            gia = 0;
            hieuung = "";
            tile = 0;
        }
        public void setHieuUng(string hu, int tl)
        {
            hieuung = hu;
            tile = tl;
        }
        public void set(float st, float td, float tx, float g)
        {
            satthuong = st;
            tocdo = td;
            tamxa = tx;
            gia = g;
        }
    }
}
